﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000314 RID: 788
	internal class TakeObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x0600107F RID: 4223 RVA: 0x0004C0F0 File Offset: 0x0004A4F0
		public TakeObservable(IObservable<T> source, int count) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.count = count;
		}

		// Token: 0x06001080 RID: 4224 RVA: 0x0004C10C File Offset: 0x0004A50C
		public TakeObservable(IObservable<T> source, TimeSpan duration, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread || source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.duration = duration;
			this.scheduler = scheduler;
		}

		// Token: 0x06001081 RID: 4225 RVA: 0x0004C13D File Offset: 0x0004A53D
		public IObservable<T> Combine(int count)
		{
			return (this.count > count) ? new TakeObservable<T>(this.source, count) : this;
		}

		// Token: 0x06001082 RID: 4226 RVA: 0x0004C15D File Offset: 0x0004A55D
		public IObservable<T> Combine(TimeSpan duration)
		{
			return (!(this.duration <= duration)) ? new TakeObservable<T>(this.source, duration, this.scheduler) : this;
		}

		// Token: 0x06001083 RID: 4227 RVA: 0x0004C188 File Offset: 0x0004A588
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			//if (this.scheduler == null)
			//{
			//	return this.source.Subscribe(new TakeObservable<T>.Take(this, observer, cancel));
			//}
			return new TakeObservable<T>.Take_(this, observer, cancel).Run();
		}

		// Token: 0x040009B0 RID: 2480
		private readonly IObservable<T> source;

		// Token: 0x040009B1 RID: 2481
		private readonly int count;

		// Token: 0x040009B2 RID: 2482
		private readonly TimeSpan duration;

		// Token: 0x040009B3 RID: 2483
		internal readonly IScheduler scheduler;

		// Token: 0x02000315 RID: 789
		private class Take : OperatorObserverBase<T, T>
		{
			// Token: 0x06001084 RID: 4228 RVA: 0x0004C1B6 File Offset: 0x0004A5B6
			public Take(TakeObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.rest = parent.count;
			}

			// Token: 0x06001085 RID: 4229 RVA: 0x0004C1CC File Offset: 0x0004A5CC
			public override void OnNext(T value)
			{
				if (this.rest > 0)
				{
					this.rest--;
					this.observer.OnNext(value);
					if (this.rest == 0)
					{
						try
						{
							this.observer.OnCompleted();
						}
						finally
						{
							base.Dispose();
						}
					}
				}
			}

			// Token: 0x06001086 RID: 4230 RVA: 0x0004C238 File Offset: 0x0004A638
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001087 RID: 4231 RVA: 0x0004C270 File Offset: 0x0004A670
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040009B4 RID: 2484
			private int rest;
		}

		// Token: 0x02000316 RID: 790
		private class Take_ : OperatorObserverBase<T, T>
		{
			// Token: 0x06001088 RID: 4232 RVA: 0x0004C2A8 File Offset: 0x0004A6A8
			public Take_(TakeObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001089 RID: 4233 RVA: 0x0004C2C4 File Offset: 0x0004A6C4
			public IDisposable Run()
			{
				//IDisposable disposable = this.parent.scheduler.Schedule(this.parent.duration, new Action(this.Tick));
				//IDisposable disposable2 = this.parent.source.Subscribe(this);
				//return StableCompositeDisposable.Create(disposable, disposable2);
				return null;
			}

			// Token: 0x0600108A RID: 4234 RVA: 0x0004C314 File Offset: 0x0004A714
			private void Tick()
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x0600108B RID: 4235 RVA: 0x0004C370 File Offset: 0x0004A770
			public override void OnNext(T value)
			{
				object obj = this.gate;
				lock (obj)
				{
					this.observer.OnNext(value);
				}
			}

			// Token: 0x0600108C RID: 4236 RVA: 0x0004C3B4 File Offset: 0x0004A7B4
			public override void OnError(Exception error)
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x0600108D RID: 4237 RVA: 0x0004C410 File Offset: 0x0004A810
			public override void OnCompleted()
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x040009B5 RID: 2485
			private readonly TakeObservable<T> parent;

			// Token: 0x040009B6 RID: 2486
			private readonly object gate = new object();
		}
	}
}
